package com.huika.dealontop.control.myinfo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huika.dealontop.R;
import com.huika.dealontop.control.base.BaseAda;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.HashSet;

/**
 * auther: chenyuan.
 * time: 2015/11/16 16:16
 * description:选择优惠券适配器
 */
public class SelectBonusAdapter extends BaseAda<HashMap<String, String>> {

    public int relPosition = -1;
    private String mHasSelectCouponNum;
    public HashSet<String> datas=new HashSet<String>();

    public SelectBonusAdapter(Context context,String hasSelectCouponNum) {
        super(context);
        mHasSelectCouponNum=hasSelectCouponNum;
    }
    public String selectId;


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder vh = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_use_bonus, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        HashMap<String, String> bean = getItem(position);

        vh.num_tv.setText(bean.get("couponQuantity") + mContext.getString(R.string.use_bonus_unit));
        vh.rb_price_tv.setText(new DecimalFormat("#").format(Double.parseDouble(bean.get("vouchersAmount"))));

        if (bean.get("vouchersAmount").equals(mHasSelectCouponNum)){
            vh.num_cb.setChecked(true);
            vh.modify_tv.setText(mContext.getString(R.string.left_cancle));
            datas.add(bean.get("vouchersId"));
            selectId=bean.get("vouchersId");
        }

        vh.general_loy.setOnClickListener(new MyClickListener(vh,position));

        return convertView;
    }

    class ViewHolder {
        public CheckBox num_cb;
        public TextView num_tv;
        public TextView modify_tv;
        public TextView rb_price_tv;
        public RelativeLayout general_loy;

        public ViewHolder(View convertView) {
            num_cb = (CheckBox) convertView.findViewById(R.id.num_cb);
            num_tv = (TextView) convertView.findViewById(R.id.num_tv);
            modify_tv = (TextView) convertView.findViewById(R.id.modify_tv);
            rb_price_tv = (TextView) convertView.findViewById(R.id.rb_price_tv);
            general_loy = (RelativeLayout) convertView.findViewById(R.id.general_loy);
        }
    }

    class MyClickListener implements View.OnClickListener {
        ViewHolder vh;
        int posit;
        public MyClickListener(ViewHolder viewHolder,int position) {
            vh = viewHolder;
            posit=position;
        }

        @Override
        public void onClick(View v) {
            if (vh.num_cb.isChecked()) {
//                        num_cb.setBackgroundResource(R.mipmap.common_unselect_choise_icon);
                vh.num_cb.setChecked(false);
                vh.modify_tv.setText(mContext.getString(R.string.use_bonus_select));
                datas.remove(getItem(posit).get("vouchersId"));
                selectId="";
                Log.d("cy", "selected"+datas);
            } else {

//                        num_cb.setBackgroundResource(R.mipmap.common_select_choise_icon);
                vh.num_cb.setChecked(true);
                vh.modify_tv.setText(mContext.getString(R.string.left_cancle));
                datas.add(getItem(posit).get("vouchersId"));
                selectId=getItem(posit).get("vouchersId");
                Log.d("cy", "no_selected"+datas);
            }
            /**保证永远只是一个*/
            if (datas.size()>1){
                vh.num_cb.setChecked(false);
                vh.modify_tv.setText(mContext.getString(R.string.use_bonus_select));
                datas.remove(getItem(posit).get("vouchersId"));
            }
        }
    }

}
